<template><div><h2 id="编辑器和集成开发环境" tabindex="-1"><a class="header-anchor" href="#编辑器和集成开发环境"><span>编辑器和集成开发环境</span></a></h2>
<h2 id="首选ide-phpstorm" tabindex="-1"><a class="header-anchor" href="#首选ide-phpstorm"><span>首选IDE: phpstorm</span></a></h2>
<p>作者推荐使用</p>
<h2 id="ide-自定义组件代码自动完成" tabindex="-1"><a class="header-anchor" href="#ide-自定义组件代码自动完成"><span>IDE 自定义组件代码自动完成</span></a></h2>
<p>现在使用 IDE 进行开发是非常常见的，因为它提供了舒适的环境。它可以检测错别字和错误，建议代码改进，当然还提供代码自动完成功能。对于 Yii 2.0 来说，它开箱即用的效果非常好，但对于自定义的应用程序组件却不是这样，例如 <code v-pre>Yii::$app-&gt;mycomponent-&gt;something</code>。</p>
<h3 id="官方处理" tabindex="-1"><a class="header-anchor" href="#官方处理"><span>官方处理</span></a></h3>
<p>从 Yii 2.0.44 版本开始，官方在应用模板中对这个问题做了处理，在 <code v-pre>config</code> 目录下，新增了一个 <code v-pre>__autocomplete.php</code> 文件，对于基础模板，它位于项目根目录 config 下面，对于高级应用模板，它位于 common/config 目录下。对于新安装模板的用户，可以找到这个文件，轻松解决问题，我们只需要配置 <code v-pre>@property</code> 这些而已。示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 这个类只存在于 IDE (PHPStorm/Netbeans/…) 的自动完成。</span>
<span class="line"> * 此文件从未包含在任何地方。</span>
<span class="line"> * 调整此文件以匹配应用程序配置中配置的类，从而为自定义组件启用 IDE 自动完成功能。</span>
<span class="line"> * 例如：一个 phpdoc 属性可以在 `__Application` 类中添加为 `@property <span class="token class-name"><span class="token punctuation">\</span>vendor<span class="token punctuation">\</span>package<span class="token punctuation">\</span>Rollbar<span class="token punctuation">|</span></span>__Rollbar $ Rollbar`，并在这个文件中添加一个类</span>
<span class="line"> * ```php</span>
<span class="line"> * // @property <span class="token class-name">of</span> \vendor\package\Rollbar goes here</span>
<span class="line"> * class __Rollbar <span class="token punctuation">{</span></span>
<span class="line"> * <span class="token punctuation">}</span></span>
<span class="line"> * ```</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Yii</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Application<span class="token punctuation">|</span><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>console<span class="token punctuation">\</span>Application<span class="token punctuation">|</span></span>__Application</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token variable">$app</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name">yii<span class="token punctuation">\</span>rbac<span class="token punctuation">\</span>DbManager</span> <span class="token parameter">$authManager</span></span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>User<span class="token punctuation">|</span></span>__WebUser $user</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>Connection</span> <span class="token parameter">$redis</span> 官方 yii2-redis 扩展的 redis 组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>Connection</span> <span class="token parameter">$mongodb</span> 官方 yii2-mongodb 扩展 的 mongodb 组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>Queue</span> <span class="token parameter">$queue</span> 官方 yii2-queue 扩展的 queue 组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>elasticsearch<span class="token punctuation">\</span>Connection</span> <span class="token parameter">$elasticsearch</span> 官方 yii2-elasticsearch 扩展的 elasticsearch 组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>sphinx<span class="token punctuation">\</span>Connection</span> <span class="token parameter">$sphinx</span> 官方 yii2-sphinx 扩展的 sphinx 组件</span>
<span class="line"> *</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">__Application</span> <span class="token punctuation">{</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name">app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>User</span> <span class="token parameter">$identity</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">__WebUser</span> <span class="token punctuation">{</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：对于已经安装了旧版的应用模板的用户，虽然没有官方提供的 <code v-pre>config/__autocomplete.php</code> 这个文件，但是您可以参考以下步骤，自行创建这个类似的文件。</p>
</blockquote>
<h3 id="使用自定义-yii-类" tabindex="-1"><a class="header-anchor" href="#使用自定义-yii-类"><span>使用自定义 Yii 类</span></a></h3>
<p>给 IDE 一些提示的最好方法是使用你自己的 <code v-pre>Yii</code> 文件，它在运行代码时实际上并不使用。这个文件可以命名为 <code v-pre>Yii.php</code>，且它的内容可以如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Yii bootstrap file.</span>
<span class="line"> * Used for enhanced IDE code autocompletion.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Yii</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>BaseYii</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name">BaseApplication<span class="token punctuation">|</span>WebApplication<span class="token punctuation">|</span>ConsoleApplication</span> the application instance</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token variable">$app</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Class BaseApplication</span>
<span class="line"> * Used for properties that are identical for both WebApplication and ConsoleApplication</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>RbacManager</span> <span class="token parameter">$authManager</span> 此应用的身份验证管理器，如果没有配置过，将返回 Null ，这个属性是只读的， 扩展组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>Mailer</span> <span class="token parameter">$mailer</span> 邮件程序组件. 这个属性是只读的， 扩展组件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>Connection</span> <span class="token parameter">$redis</span> 官方 yii2-redis 扩展的 redis 组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>Connection</span> <span class="token parameter">$mongodb</span> 官方 yii2-mongodb 扩展 的 mongodb 组件</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>Queue</span> <span class="token parameter">$queue</span> 官方 yii2-queue 扩展的 queue 组件</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">BaseApplication</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Application</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Class WebApplication</span>
<span class="line"> * Include only Web application related components here</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>User</span> <span class="token parameter">$user</span> The user component. This property is read-only. Extended component.</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>MyResponse</span> <span class="token parameter">$response</span> The response component. This property is read-only. Extended component.</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>ErrorHandler</span> <span class="token parameter">$errorHandler</span> The error handler application component. This property is read-only. Extended component.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">WebApplication</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Application</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Class ConsoleApplication</span>
<span class="line"> * Include only Console application related components here</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>components<span class="token punctuation">\</span>ConsoleUser</span> <span class="token parameter">$user</span> The user component. This property is read-only. Extended component.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ConsoleApplication</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>console<span class="token punctuation">\</span>Application</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的 <code v-pre>BaseApplication</code>, <code v-pre>WebApplication</code>, <code v-pre>ConsoleApplication</code> 的 PHPDoc 中，将被 IDE 用于自动完成那些通过 <code v-pre>@property</code> 描述的自定义组件。例如： <code v-pre>Yii::$app-&gt;redis</code> 、 <code v-pre>Yii::$app-&gt;mongodb</code> 、 <code v-pre>Yii::$app-&gt;queue</code> 等等。</p>
<blockquote>
<p><strong>注意</strong>：To avoid “Multiple Implementations” PHPStorm warning and make autocomplete faster exclude or “Mark as Plain Text” <code v-pre>vendor/yiisoft/yii2/Yii.php</code> file.</p>
</blockquote>
<p>就是这样，现在 <code v-pre>Yii::$app-&gt;user</code> 将会是我们的 <code v-pre>\app\components\User</code> 组件来代替之前系统默认的。这同样适用于所有其他 <code v-pre>@property</code> 声明的组件。</p>
<h3 id="自定义-yii-类自动生成" tabindex="-1"><a class="header-anchor" href="#自定义-yii-类自动生成"><span>自定义 Yii 类自动生成</span></a></h3>
<p>您可以使用应用程序配置中的组件定义来自动生成自定义 <code v-pre>Yii</code> 类。<br>
查看 <a href="https://github.com/bazilio91/yii2-stubs-generator" target="_blank" rel="noopener noreferrer">bazilio91/yii2-stubs-generator</a> 扩展.</p>
<h3 id="用户自定义组件" tabindex="-1"><a class="header-anchor" href="#用户自定义组件"><span>用户自定义组件</span></a></h3>
<p>为了获得针对用户身份的自动完成，即 <code v-pre>Yii::$app-&gt;user-&gt;identity</code> ， <code v-pre>app\components\User</code> 类应该如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * <span class="token keyword">@inheritdoc</span></span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@property</span> <span class="token class-name"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>User<span class="token punctuation">|</span><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>IdentityInterface<span class="token punctuation">|</span><span class="token keyword">null</span></span> <span class="token parameter">$identity</span> The identity object associated with the currently logged-in user. null is returned if the user is not logged in (not authenticated).</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>User</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>因此，Yii 的配置文件中针对组件，可能看起来是这样的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token doc-comment comment">/**</span>
<span class="line">         * User</span>
<span class="line">         */</span></span>
<span class="line">        <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\User'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'identityClass'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\models\User'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token doc-comment comment">/**</span>
<span class="line">         * Custom Component</span>
<span class="line">         */</span></span>
<span class="line">        <span class="token string single-quoted-string">'response'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\MyResponse'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


